package com.yanqu.road.server.cmd.monopolymarket.fortress;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.MonopolyMarketCommonMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @Description
 * @Author cwq
 * @Data 2021/10/15 16:32
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_FORTRESS_ENTER_CITY, desc = "进入榷场边关要塞 城池")
public class MonopolyMarketFortressEnterCityCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketEnterFortressCityReqMsg req = MonopolyMarketProto.MonopolyMarketEnterFortressCityReqMsg.parseFrom(packet.getBytes());
        if(!MonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!MonopolyMarketCommonMgr.isSystemOpen(MonopolyMarketMgr.group, eMonopolyMarketResType.FORTRESS.getValue())){
            // 休息期不能进入
            ret = GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        // 判断城池id 是否存在
        if(!MonopolyMarketMgr.fortressResConfigMap.containsKey(req.getId())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_RES_FORTRESS_CITY_ID_NO_EXIST;
            return;
        }
        // 派遣期传玩家所在的当前联盟
        String unionId = MonopolyMarketMgr.getJoinFortressUnionId(packet.getPlayerId());
        MonopolyMarketProto.MonopolyMarketEnterFortressCityToCrossReqMsg.Builder toCrossReqMsg = MonopolyMarketProto.MonopolyMarketEnterFortressCityToCrossReqMsg.newBuilder();
        toCrossReqMsg.setCtsMsg(req);
        toCrossReqMsg.setUnionId(unionId);

        player.sendPacket(CrossProtocol.C_MONOPOLY_MARKET_FORTRESS_ENTER_CITY, toCrossReqMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyMarketProto.MonopolyMarketEnterFortressCityRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketEnterFortressCityRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_FORTRESS_ENTER_CITY, builder);
        }
    }

}
